package com.chalk.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chalk.common.util.Result;
import com.chalk.dao.CollectionDao;
import com.chalk.model.Collection;
import com.chalk.service.CollectionService;

import com.chalk.service.VenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author glq
 * @since 2020-12-17
 */

@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionDao, Collection> implements CollectionService {

    @Autowired
    VenueService venueService;

    @Override
    public int addCollection(Collection entity) {
        return baseMapper.insert(entity);
    }

    @Override
    public int updateCollection(Collection entity) {
        return baseMapper.updateById(entity);
    }


    @Override
    public int deleteCollection(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public Result collectionVenue(Integer venueId, String memberId) {
        //检测场馆是否存在
        Boolean fz = venueService.isVenueExist(venueId);
        if (!fz) {
            return Result.fail("该场馆不存在！");
        }
        //检查场馆是否被收藏
        Boolean f = isCollectionVenue(venueId, memberId);
        if (!f) {
            return Result.fail("该场馆已经被收藏!");
        }

        Collection collection = new Collection();
        collection.setMemberId(memberId);
        collection.setVenueId(venueId);
        collection.setCreateTime(LocalDateTime.now());
        Boolean zf = insert(collection);
        if (zf) {
            return Result.success("场馆收藏成功！");
        }
        return Result.fail("场馆收藏失败！");


    }

    @Override
    public Boolean isCollectionVenue(Integer venueId, String memberId) {
        //构造器
        EntityWrapper<Collection> wrapper = new EntityWrapper<>();
        wrapper.eq("venue_id", venueId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("is_delete", 0);
        Collection collection = selectOne(wrapper);
        if (collection == null) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public Result cancelCollection(Integer venueId, String memberId) {
        //检测场馆是否存在
        Boolean fz = venueService.isVenueExist(venueId);
        if (!fz) {
            return Result.fail("该场馆不存在！");
        }
        //检查场馆是否被收藏
        Boolean f = isCollectionVenue(venueId, memberId);
        if (f) {
            return Result.fail("该场馆没有被收藏，无法取消!");
        }
        //构造器
        EntityWrapper<Collection> wrapper = new EntityWrapper<>();
        wrapper.eq("venue_id", venueId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("is_delete", 0);
        Collection collection = new Collection();
        collection.setIsDelete(1);//被删除
        Boolean zf = update(collection, wrapper);
        if (zf) {
            return Result.success("场馆取消收藏成功！");
        }
        return Result.success("场馆取消收藏失败！");
    }

    @Override
    public List<Integer> getVenueIdsByMemberId(String memberId) {
        //构造器
        EntityWrapper<Collection> wrapper = new EntityWrapper<>();
        wrapper.eq("member_id", memberId);
        wrapper.eq("is_delete", 0);
        List<Collection> collections=selectList(wrapper);
        List<Integer> list=new ArrayList<>();
        for(Collection c:collections){
            list.add(c.getVenueId());
        }
        return list;
    }
}
